package com.rrd.hive.udf;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.io.Text;
/**
*按工作日对日期进行加计算
*@author liubaoxin
*@date 2020年5月12日
*/

@Description(name = "wor_date_add",
    value = "_FUNC_(start_date, num_days) - Returns the date that is num_days after start_date.",
    extended = "start_date is a string in the format 'yyyy-MM-dd HH:mm:ss' or"
        + " 'yyyy-MM-dd'. num_days is a number. The time part of start_date is "
        + "ignored.\n"
        + "Example:\n "
        + "  > SELECT _FUNC_('2009-07-30', 1) FROM src LIMIT 1;\n"
        + "  '2009-07-31'")
public class WorkDateAddUDF2 extends   UDF{

	private transient SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	private final  Calendar calendar = Calendar.getInstance();
	private final Text output = new Text();
	private static Set<String> daySet= new HashSet<String>();
	private transient StringBuilder sb = new StringBuilder();
	public Text evaluate(String start_date,String end_date) throws HiveException, ParseException {
		calendar.setTime(formatter.parse(start_date));
		if(!daySet.contains(start_date)){
			this.sb.append(start_date).append(",");
		} 
		while(start_date.compareTo(end_date)<0){
			start_date=addDay(1);  
			if(daySet.contains(start_date)){
				continue;
			}
			this.sb.append(start_date).append(",");
		 
//			System.out.println(start_date);
		} 
		output.set(sb.toString());
		return output;
	}
	private   String addDay(int n){
		
		calendar.add(Calendar.DAY_OF_MONTH, n);
		return formatter.format(calendar.getTime());
		
	}
	public static void main(String[] args) throws HiveException, ParseException {
		WorkDateAddUDF2 workDay = new WorkDateAddUDF2();
		System.out.println(workDay.evaluate("2020-04-28", "2020-05-05"));;
	}
	static{
		daySet.add("2020-01-01");
		daySet.add("2020-01-04");
		daySet.add("2020-01-05");
		daySet.add("2020-01-11");
		daySet.add("2020-01-12");
		daySet.add("2020-01-18");
		daySet.add("2020-01-24");
		daySet.add("2020-01-25");
		daySet.add("2020-01-26");
		daySet.add("2020-01-27");
		daySet.add("2020-01-28");
		daySet.add("2020-01-29");
		daySet.add("2020-01-30");
		daySet.add("2020-02-02");
		daySet.add("2020-02-08");
		daySet.add("2020-02-09");
		daySet.add("2020-02-15");
		daySet.add("2020-02-16");
		daySet.add("2020-02-22");
		daySet.add("2020-02-23");
		daySet.add("2020-02-29");
		daySet.add("2020-03-01");
		daySet.add("2020-03-07");
		daySet.add("2020-03-08");
		daySet.add("2020-03-14");
		daySet.add("2020-03-15");
		daySet.add("2020-03-21");
		daySet.add("2020-03-22");
		daySet.add("2020-03-28");
		daySet.add("2020-03-29");
		daySet.add("2020-04-04");
		daySet.add("2020-04-05");
		daySet.add("2020-04-06");
		daySet.add("2020-04-11");
		daySet.add("2020-04-12");
		daySet.add("2020-04-18");
		daySet.add("2020-04-19");
		daySet.add("2020-04-25");
		daySet.add("2020-05-01");
		daySet.add("2020-05-02");
		daySet.add("2020-05-03");
		daySet.add("2020-05-04");
		daySet.add("2020-05-05");
		daySet.add("2020-05-10");
		daySet.add("2020-05-16");
		daySet.add("2020-05-17");
		daySet.add("2020-05-23");
		daySet.add("2020-05-24");
		daySet.add("2020-05-30");
		daySet.add("2020-05-31");
		daySet.add("2020-06-06");
		daySet.add("2020-06-07");
		daySet.add("2020-06-13");
		daySet.add("2020-06-14");
		daySet.add("2020-06-20");
		daySet.add("2020-06-21");
		daySet.add("2020-06-25");
		daySet.add("2020-06-26");
		daySet.add("2020-06-27");
		daySet.add("2020-07-04");
		daySet.add("2020-07-05");
		daySet.add("2020-07-11");
		daySet.add("2020-07-12");
		daySet.add("2020-07-18");
		daySet.add("2020-07-19");
		daySet.add("2020-07-25");
		daySet.add("2020-07-26");
		daySet.add("2020-08-01");
		daySet.add("2020-08-02");
		daySet.add("2020-08-08");
		daySet.add("2020-08-09");
		daySet.add("2020-08-15");
		daySet.add("2020-08-16");
		daySet.add("2020-08-22");
		daySet.add("2020-08-23");
		daySet.add("2020-08-29");
		daySet.add("2020-08-30");
		daySet.add("2020-09-05");
		daySet.add("2020-09-06");
		daySet.add("2020-09-12");
		daySet.add("2020-09-13");
		daySet.add("2020-09-19");
		daySet.add("2020-09-20");
		daySet.add("2020-09-26");
		daySet.add("2020-10-01");
		daySet.add("2020-10-02");
		daySet.add("2020-10-03");
		daySet.add("2020-10-04");
		daySet.add("2020-10-05");
		daySet.add("2020-10-06");
		daySet.add("2020-10-07");
		daySet.add("2020-10-08");
		daySet.add("2020-10-11");
		daySet.add("2020-10-17");
		daySet.add("2020-10-18");
		daySet.add("2020-10-24");
		daySet.add("2020-10-25");
		daySet.add("2020-10-31");
		daySet.add("2020-11-01");
		daySet.add("2020-11-07");
		daySet.add("2020-11-08");
		daySet.add("2020-11-14");
		daySet.add("2020-11-15");
		daySet.add("2020-11-21");
		daySet.add("2020-11-22");
		daySet.add("2020-11-28");
		daySet.add("2020-11-29");
		daySet.add("2020-12-05");
		daySet.add("2020-12-06");
		daySet.add("2020-12-12");
		daySet.add("2020-12-13");
		daySet.add("2020-12-19");
		daySet.add("2020-12-20");
		daySet.add("2020-12-26");
		daySet.add("2020-12-27");
		
	}
}
